package com.ruoyi.wx.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.system.service.ISysOssService;
import com.ruoyi.wx.entity.PtScore;
import com.ruoyi.wx.entity.PtTeacherClass;
import com.ruoyi.wx.entity.PtVideo;
import com.ruoyi.wx.mapper.PtScoreMapper;
import com.ruoyi.wx.mapper.PtTeacherClassMapper;
import com.ruoyi.wx.mapper.PtVideoMapper;
import com.ruoyi.wx.service.IPtVideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 视频表 服务实现类
 * </p>
 *
 * @author moxiu
 * @since 2022-03-04
 */
@Service
public class PtVideoServiceImpl extends ServiceImpl<PtVideoMapper, PtVideo> implements IPtVideoService {

    @Autowired
    private PtScoreMapper ptScoreMapper;

    @Autowired
    private PtTeacherClassMapper ptTeacherClassMapper;

    @Autowired
    private ISysOssService iSysOssService;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public int recordWatch(Long videoId, Long expId) {
        Long userId = SecurityUtils.getUserId();
        PtScore ptScore = ptScoreMapper.selectOne(Wrappers.lambdaQuery(PtScore.class)
            .eq(PtScore::getExpId, expId).eq(PtScore::getUserId, userId).eq(PtScore::getStatus, "0")
            .select(PtScore::getVideoList,PtScore::getUserId)
        );
        if (ptScore == null) {
            Long deptId = SecurityUtils.getDeptId();
            Long teacherId = ptTeacherClassMapper.getTeacherId(deptId);
            //如果成绩卡为空 则 创建当前用户的实验卡
            PtScore createScore = new PtScore();
            createScore.setExpId(expId);
            createScore.setUserId(userId);
            createScore.setTeacherId(teacherId);
            ptScoreMapper.insert(createScore);
        }
        //记录用户看的视频
        String s1 = ptScoreMapper.selectVideoList(userId,expId);
        String s = null;
        int a = Math.toIntExact(videoId);
        if (s1 == null) {
            Set<Integer> set = new HashSet<>();
            set.add(a);
            s = JSONObject.toJSONString(set);
        }else {
            List<Integer> parse = (List<Integer>)JSONObject.parse(s1);
            Set<Integer> result = new HashSet<>(parse);
            result.add(a);
            s = JSONObject.toJSONString(result);
        }
        //更新用户所看视频的id集合
        return ptScoreMapper.update(null,Wrappers.lambdaUpdate(new PtScore())
            .eq(PtScore::getExpId,expId).eq(PtScore::getUserId,userId)
            .set(PtScore::getVideoList,s)
        );
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void uploadVideo(MultipartFile file, Long expId) {
        PtVideo ptVideo = new PtVideo();
        ptVideo.setExpId(expId);
        ptVideo.setUrl(iSysOssService.upload(file).getUrl());
        baseMapper.insert(ptVideo);
    }
}
